import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Req,
  Query,
  Request,
} from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { AuthGuard } from '@nestjs/passport';
import { ApiTags } from '@nestjs/swagger';

@ApiTags('用户模块')  
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post('/register')
  create(@Body() createUserDto: CreateUserDto) {
    //开启事务第三步：获取事务管理器)
    return this.userService.create(createUserDto);
  }

  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  @Get('list')
  async findAll(@Query('username') username: string) {
    const [data, count] = await this.userService.findAll(username);
    return { count, data };
  }

  @Get('/self')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  findOwner(@Req() req) {
    return this.userService.findOne(req.user.userid);
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.userService.findOne(id);
  }

  @Patch(':id')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(id, updateUserDto);
  }

  @Delete(':id')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  remove(@Request() req, @Param('id') id: string) {
    return this.userService.remove(id, req.user);
  }




}
